<template>
  <div style="padding: 10px;">
    <Row :space="15">
      <Cell>
        <Input v-model="formData.parameterName" placeholder="参数名称" style="width: 100%;" />
      </Cell>
      <Cell>
        <Input v-model="formData.name" placeholder="节点名称" style="width: 100%;" />
      </Cell>
      <Cell>
        <Select v-model="formData.model" :datas="models" keyName="value" titleName="label" placeholder="选择模型"></Select>
      </Cell>
      <Cell>
        <Select v-model="formData.collectionId" :datas="collectionList" @change="collectionChange"
          placeholder="知识库集合"></Select>
      </Cell>
      <Cell>
        <Select v-model="formData.filename" :datas="filenameList" :title="formData.filename"
          placeholder="知识库文件"></Select>
      </Cell>
      <Cell>
        <NumberInput v-model="formData.limit" :useOperate="true" placeholder="检索数量"></NumberInput>
      </Cell>
      <Cell>
        <Slider v-model="formData.similarityScore" :range='{ start: 0, end: 10 }'
          :title="'相似比例:' + formData.similarityScore" />
      </Cell>
      <Cell>
        <Checkbox v-model="formData.analyse">模型分析</Checkbox>
      </Cell>
      <Cell>
        <Textarea v-model="formData.system" rows="5" placeholder="系统提示词" style="width: 100%;"
          :readonly="!formData.analyse"></Textarea>
      </Cell>
      <Cell>
        <Textarea v-model="formData.message" rows="5" placeholder="输入问题" style="width: 100%;"></Textarea>
      </Cell>
      <Cell>
        <Checkbox v-model="formData.printInput" placeholder="跟踪打印" title="跟踪打印">打印输入</Checkbox>
        <Space :width="15" />
        <Checkbox v-model="formData.printOutput" title="跟踪打印">打印输出</Checkbox>
        <Space :width="15" />
        <Checkbox v-model="formData.inputHistory" title="会话历史">保存输入</Checkbox>
        <Space :width="15" />
        <Checkbox v-model="formData.resultHistory" title="会话历史">保存输出</Checkbox>
      </Cell>
      <Cell>
        <Button color="primary" @click="bindForm">设置</Button>
        <Space :width="15" />
        <Button @click="setForm" color="yellow">重置</Button>
      </Cell>
    </Row>
  </div>
</template>

<script>
import LogicFlow from '@logicflow/core';
import { message } from 'heyui';

export default {
  props: {
    lf: Object,
    currentNode: Object,
  },
  mounted() {
    this.getModel();
    this.getCollectionList();
    this.getFilenameList();
    this.setForm();
    this.getParameters();
  },
  data() {
    return {
      models: [],
      collectionList: [],
      filenameList: [],
      parameters: [],
      formData: {
        collectionId: "",
        limit: 5,
        filename: "",
        collectionName: "",
        message: "",
        resultHistory: true,
        similarityScore: 7,
      }
    }
  },
  methods: {
    setForm() {
      this.formData.name = this.$props.currentNode.data.text.value;
      let id = this.$props.currentNode.data.id;
      let properties = this.$props.lf.getProperties(id);
      this.formData.model = properties.model;
      this.formData.printInput = properties.printInput;
      this.formData.printOutput = properties.printOutput;
      if (!!properties.limit) {
        this.formData.limit = properties.limit;
      }
      this.formData.collectionId = properties.collectionId;
      this.formData.collectionName = properties.collectionName;
      this.formData.system = properties.system;
      this.formData.message = properties.message;
      this.formData.parameterName = properties.parameterName;
      this.formData.filename = properties.filename;
      this.formData.inputHistory = properties.inputHistory;
      if (!!properties.resultHistory) {
        this.formData.resultHistory = properties.resultHistory;
      }
      this.formData.analyse = properties.analyse;
      if (properties.similarityScore > 0) {
        this.formData.similarityScore = properties.similarityScore * 10;
      }
    },
    bindForm() {
      let id = this.$props.currentNode.data.id;
      this.$props.lf.updateText(id, this.formData.name);
      this.$props.lf.setProperties(id, Object.assign({ ...this.formData }, {
        similarityScore: Number((this.formData.similarityScore * 0.1).toFixed(2)),
      }));
      this.$emit('close', false);
      message.success("设置成功");
    },
    getModel() {
      this.$get("/chat/getModel").then(res => {
        if (res.data.code == this.$success) {
          this.models = res.data.data;
        } else {
          message.error(res.data.msg);
        }
      });
    },
    getFilenameList() {
      this.$post("/ollamaembedding/list", { vars: { "a.collection_id": this.formData.collectionId }, page: { current: 1, size: 1000 } }).then(res => {
        if (res.data.code == this.$success) {
          this.filenameList = res.data.data.rows;
        } else {
          this.filenameList = [];
          message.error(res.data.msg);
        }
      })
    },
    getCollectionList() {
      this.$post("/ollamacollection/list", { page: { current: 1, size: 1000 } }).then(res => {
        if (res.data.code == this.$success) {
          this.collectionList = res.data.data.rows;
          if (!!this.collectionId) {
            this.getFilenameList();
          }
        } else {
          message.error(res.data.msg);
        }
      })
    },
    collectionChange(data) {
      this.filename = "";
      this.formData.collectionName = data.label;
      this.getFilenameList(data.value);
    },

    getParameters() {
      let flowData = (this.$props.lf).getGraphRawData();
      let nodes = flowData["nodes"];
      let node = this.getStartNode(flowData);
      while (!!node && node?.id != this.$props.currentNode?.data?.id && node?.type != 'EndNode') {
        var properties = node?.properties;
        var name = properties?.name;
        var parameterName = properties?.parameterName;

        if (!!parameterName) {
          if (properties?.inputHistory == true) {
            this.parameters.push({ label: "$" + parameterName + ".input", value: "$" + parameterName + ".input" });
          } if (properties?.resultHistory == true) {
            this.parameters.push({ label: "$" + parameterName + ".output", value: "$" + parameterName + ".output" });
          }
        }
        node = this.getNextNode(flowData, node.id);
      }
    },
    getStartNode(flowData) {
      let nodes = flowData["nodes"];
      for (var i = 0; i < nodes.length; i++) {
        let node = nodes[i];
        if (node.type == 'StartNode') {
          return node;
        }
      }
    },
    getNode(flowData, id) {
      let nodes = flowData["nodes"];
      for (var i = 0; i < nodes.length; i++) {
        let node = nodes[i];
        if (node.id == id) {
          return node;
        }
      }
    },
    getNextNode(flowData, id) {
      let edges = flowData["edges"]
      for (var i = 0; i < edges.length; i++) {
        let edge = edges[i];
        if (edge.sourceNodeId == id) {
          return this.getNode(flowData, edge.targetNodeId);
        }
      }
    },
  }
}
</script>